﻿using Model;
using NLog;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    [ObjectEvent]
    public class TestComponentEvent : ObjectEvent<TestComponent>, ILoad,IAwake
    {
        public void Awake()
        {
          this.Get().Awake();
        }

        public void Load()
        {
            Log.Info(Type().Name + "重新加载成功！");
        }
    }

    public class TestComponent : Component
    {
        Logger log = LogManager.GetCurrentClassLogger();
        PacketParser PacketParser;
        public void Awake()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 512; i++)
            {
                sb.Append("啊");
            }

            ARequest c2S_Test = new C2S_Test()
            {
                RpcId = 1,
                Account = "1",
                Password = sb.ToString()
            };
            Log.Info(c2S_Test.ToHex2());

            // 环形Buffer测试
           // RingBuffer ringBuffer = new RingBuffer();

          Circularbuffer ringBuffer = new Circularbuffer();

            var mg = ProtobufHelper.ToBytes(c2S_Test);
           // PacketParser = new PacketParser(ringBuffer);
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < 1000000; i++)
            {
                byte[] op = BitConverter.GetBytes((ushort)Opcode.C2S_Test);
                byte[] size = BitConverter.GetBytes((ushort)(op.Length + mg.Length));

                ringBuffer.SendTo(size);
                ringBuffer.SendTo(op);
                ringBuffer.SendTo(mg);
            }
            stopwatch.Stop();
            Log.Info($"完成！{stopwatch.Elapsed.TotalMilliseconds}");
            Console.WriteLine();


            //for (int i = 0; i < 10000; i++)
            //{
            //    TestAsync(i);
            //}
        }

        void TestAsync(int i)
        {
            bool b = PacketParser.Parser();
            if (b)
            {
                Packet packet = PacketParser.GetPacket();

                C2S_Test c2S_Test = ProtobufHelper.FromBytes<C2S_Test>(packet.Bytes, 2, packet.Length - 2);
                Log.Info($"{i} : {c2S_Test.ToJson()}");
            }
        }
    }
}
